package com.gd.coreservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.commonutils.Constants;
import com.gd.commonutils.Result;
import com.gd.coreservice.entity.Admin;
import com.gd.coreservice.entity.vo.AdminVo;
import com.gd.coreservice.service.AdminService;
import com.gd.coreservice.utils.TokenUtils;
import com.gd.servicebase.exceptionhandler.ServiceException;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;

    //登录  已测试
    @GetMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String password){
        if(adminService.login(username,password)!=null)
        {
            // 设置token
            String userId=adminService.findId(username);//根据用户名去查找ID
            String token = TokenUtils.genToken(userId, password);
            AdminVo adminVo = adminService.login(username,password);
            adminVo.setToken(token);
            return Result.success(adminVo);
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");//自定义异常
        }
    }

    //注册  已测试
    @PostMapping("/registerAdmin")
    public Result register(@RequestParam String username, @RequestParam String password){
        if(adminService.register(username,password))
        {
            return Result.success("注册成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "该用户名已被注册");//自定义异常
        }
    }

    //查询所有用户  已测试
    @GetMapping("/getAdmin")
    public List<Admin> getAdmin() {
        return adminService.findAll();
    }

    //根据id查询数据
    //202403024修改了  已测试
    @GetMapping("/getOneAdmin")
    public Result findOne(@RequestParam("Aid") String id){
        Admin one = adminService.findOne(id);
        return Result.success(one);
    }


    //修改个人密码
    //20240324修改了  已测试
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestParam("Aid") String id,@RequestParam String password, @RequestParam String newpassword){

        if(adminService.updatePassword(id,password, newpassword))
        {
            return Result.success("修改密码成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "修改失败：密码错误");//自定义异常
        }
    }

    //后台新增用户  已测试
    @PostMapping("/insertAdmin")
    public Result addAdmin(@RequestBody Admin admin) {
        if(adminService.insertAdmin(admin))
        {
            return Result.success("添加成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "用户名重复");//自定义异常
        }
    }
    //修改用户的基本信息 必须有id  已测试
    @PutMapping("/updateAdmin")
    public Result updateAdmin(@RequestBody Admin admin){
        if(adminService.updateAdmin(admin))
        {
            return Result.success("修改成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "用户名重复");//自定义异常
        }
    }

    //根据用户id删除用户  已测试  是逻辑删除
    @DeleteMapping("/deleteAdmin/{Aid}")
    public Integer deleteAdmin(@PathVariable("Aid") String Aid){
        return adminService.deleteAdmin(Aid);
    }

    //批量删除  已测试
    // ["1771852947365728258",
    //  "1771850558722187266",
    //  "1"]
    @PostMapping("/deleteAdmin/batch")
    public Integer deleteAdminBatch(@RequestBody List<String> Aids){
        return adminService.deleteAdminBatch(Aids);
    }

    //分页查询  还有根据用户名进行模糊查询20240324  已测试
    //这里我修改入参的类型为Long 以及返回的结构中total类型为long
    // 接口路径：/user/page?pageNum=1&pageSize=10
    @GetMapping("/getAdminPage")
    public Map<String, Object> getAdminPage(@RequestParam Long pageNum,
                                            @RequestParam Long pageSize,
                                            @RequestParam String Ausername) {
        HashMap<String, Object> resultMap = new HashMap<>();
        Map<String, Object> result = adminService.getAdminPageData(pageNum,pageSize,Ausername);
        List<Admin> data = (List<Admin>) result.get("data");
         Long total = (Long) result.get("total");
        resultMap.put("data",data);
        resultMap.put("total",total);
        return resultMap;//返回结果Map集合：{data：List<admin>集合，total：总的结果树} map集合.put添加key value
    }


    //查询总管理员数  管理员表的记录数  已测试
    @GetMapping("/getAdminAllTotal")
    public int getAllTotal(){
        return adminService.getAllTotal();
    }



}

